using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Entities;

namespace DAL
{
    public class tbDonHangDAL
    {
         private SqlConnection cn = null;
        public tbDonHangDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
        public void Insert(tbDonHang donhang)
        {
            SqlCommand cm = new SqlCommand("DonHang_Insert", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@IDDonHang ", donhang.IDDonHang);
            cm.Parameters.AddWithValue("@IDKhachHang ", donhang.IDKhachHang);
            cm.Parameters.AddWithValue("@NgayDH ", donhang.NgayDH);
            cm.Parameters.AddWithValue("@IDTTDonHang ", donhang.IDTTDonHang);
            cm.Parameters.AddWithValue("@TriGia ", donhang.TriGia);
            cm.Parameters.AddWithValue("@NgayGiaoHang ", donhang.NgayGiaoHang);
            cm.ExecuteNonQuery();

        }
        public void Update(tbDonHang donhang)
        {
            SqlCommand cm = new SqlCommand("DonHang_Update", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@IDDonHang ", donhang.IDDonHang);
            cm.Parameters.AddWithValue("@IDKhachHang ", donhang.IDKhachHang);
            cm.Parameters.AddWithValue("@NgayDH ", donhang.NgayDH);
            cm.Parameters.AddWithValue("@IDTTDonHang ", donhang.IDTTDonHang);
            cm.Parameters.AddWithValue("@TriGia ", donhang.TriGia);
            cm.Parameters.AddWithValue("@NgayGiaoHang ", donhang.NgayGiaoHang);
            cm.ExecuteNonQuery();
        }
        public void Delete(int Id)
        {
            SqlCommand cm = new SqlCommand("DonHang_Delete", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaDH", Id);
            cm.ExecuteNonQuery();
        }
        public tbDonHang.tbDonHangCollection Get(string Where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("DonHang_Get", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", Where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", System.Data.DbType.Int32)).Direction = System.Data.ParameterDirection.Output;
            SqlDataReader dr = cm.ExecuteReader();
            tbDonHang.tbDonHangCollection dhcollection = new tbDonHang.tbDonHangCollection();
            while (dr.Read())
            {
                tbDonHang dh = new tbDonHang();
                dh.IDDonHang = int.Parse(dr.GetValue(0).ToString());
                dh.IDKhachHang = int.Parse(dr.GetValue(1).ToString());
                dh.NgayDH = DateTime.Parse(dr.GetValue(2).ToString());
                dh.IDTTDonHang = int.Parse(dr.GetValue(3).ToString());
                dh.TriGia = int.Parse(dr.GetValue(4).ToString());
                dh.NgayGiaoHang = DateTime.Parse(dr.GetValue(5).ToString());
                dhcollection.Add(dh);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return dhcollection;
        }

    }

}
